//
//  ViewController.m
//  04-UIControl
//
//  Created by 王道林 on 16/7/19.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blackColor];
    // UISwitch
    // Switch 宽高不影响控件大小
    UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    [self.view addSubview:switchControl];
    // 1.开启时的控件内部颜色
    switchControl.onTintColor = [UIColor yellowColor];
    // 2.边框及切换到关闭时的颜色
    switchControl.tintColor = [UIColor cyanColor];
    //3.开关按钮的颜色(圆圈的颜色)
    switchControl.thumbTintColor = [UIColor blackColor];
    //4.打开时的图片 iOS7后失效
 //   switchControl.onImage = [UIImage imageNamed:@"u=2836014281,1543658309&fm=23&gp=0.jpg"];
    //5.关闭时的图片 iOS7后失效
 //   switchControl.offImage = [UIImage imageNamed:@"u=2992248920,985956402&fm=23&gp=0.jpg"];
    //6.判断switch的开闭状态
    //通过setter方法 改变switch的初始状态
    switchControl.on = YES;
    NSLog(@"%@", switchControl.isOn ? @"YES" : @"NO");
    [switchControl setOn:YES animated:YES];
    //7.添加绑定事件(与button相同)
    [switchControl addTarget:self action:@selector(switcAction:) forControlEvents:UIControlEventValueChanged];
    
    
    // slider
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
    // 前后设置图片
 //   slider.minimumValueImage = [UIImage imageNamed:@"2.jpg"];
 //   slider.maximumValueImage = [UIImage imageNamed:@"2.jpg"];
    // 设置最大最小值
    slider.minimumValue = 10;
    slider.maximumValue = 100;
    // 没滑到位子的颜色
    slider.maximumTrackTintColor = [UIColor redColor];
    //滑块颜色
  //  slider.thumbTintColor = [UIColor blackColor];
    // 当前滑块的值
    slider.value = 0.5f;
    //当滑动结束后 才触发事件
    slider.continuous = NO;
    // 绑定一个事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    //设置好看的slider
    // 设置进度条滑块左边的图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    // 设置进度条滑块图片
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [self.view addSubview:slider];
    
    
    UISegmentedControl * Segment = [[UISegmentedControl alloc] initWithItems:@[@"first", @"second", @"last"]];
    Segment.tag = 1000;
    // segment 不给大小, 系统会自动计算大小
 //   NSLog(@"%@", NSStringFromCGRect(Segment.frame));
    Segment.frame = CGRectMake(20, 300, 335, 50);
    [self.view addSubview:Segment];
    //设置为YES,点击显示 一下 就没了
    // 默认为NO
    Segment.momentary = YES;
    
    NSUInteger numberOfSegment = Segment.numberOfSegments;
    NSLog(@"%lu", numberOfSegment);
    // 用来修改对应下标的标题
    [Segment setTitle:@"huang xiao gou wangwang" forSegmentAtIndex:0];
    //修改指定下标的宽度  默认为0
    [Segment setWidth:200 forSegmentAtIndex:0];
    // 设置对应下标内容的偏移
    [Segment setContentOffset:CGSizeMake(20, 0) forSegmentAtIndex:0];
    // enabled 是否被选中
    [Segment setEnabled:NO forSegmentAtIndex:0];
    Segment.tintColor = [UIColor whiteColor];
    // 初始选中的位子
 //   Segment.selectedSegmentIndex = 1;
    //apportionsSegmentWidthsByContent  默认为No
    // 如果分段控制器的段宽为0 时, 并且该属性为yes时, 系统会自动计算
    Segment.apportionsSegmentWidthsByContent = YES;
    [Segment addTarget:self action:@selector(segemntAction:) forControlEvents:UIControlEventValueChanged];
    
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(200, 500, 50, 50);
    [button setTitle:@"点我添加" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    // UIPageControl
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 600, 300, 50)];
    [self.view addSubview:page];
    // 1 . 分页控制器的页数.
    page.numberOfPages = 5;
    // 2 .当前页
    page.currentPage = 2;
    //3 .当只有一页时 隐藏
    page.hidesForSinglePage = YES;
    // 4. 获取 点的大小
     CGSize sizi = [page sizeForNumberOfPages:0];
    NSLog(@"%@", NSStringFromCGSize(sizi));
    //5 . 未选中页的颜色
    page.pageIndicatorTintColor = [UIColor greenColor];
    
    // 6. 选中颜色
    page.currentPageIndicatorTintColor = [UIColor redColor];
    // 7 . 绑定事件
    [page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    // 8 . 关闭系统事件page的切换. 直到调用updateCurrentPageDisplay的方法才会显示
    page.defersCurrentPageDisplay = YES;
    [page updateCurrentPageDisplay];
    
}
- (void)switcAction:(UISwitch *)swit
{
    //根据switch的状态 决定触发什么样的事件
    if (swit.on == YES) {
        NSLog(@"ON");
    } else {
        NSLog(@"关");
    }
}
- (void)sliderAction:(UISlider *)slider
{
    NSLog(@"滑动");
}

//
- (void)buttonAction:(UIButton *)button
{
    UISegmentedControl *segment = [self.view viewWithTag:1000];
    [segment insertSegmentWithTitle:@"wanghuang" atIndex:0 animated:YES];
}

//
- (void)segemntAction:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 1:
            NSLog(@"1");
            break;
        case 2:
            NSLog(@"2");
            break;
        default:
            break;
    }
}

//
- (void)pageAction:(UIPageControl *)page
{
    NSLog(@"wang");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
